package com.maimang.platform.dsp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.maimang.platform.common.utils.RedisUtils;
import com.maimang.platform.dsp.constant.TokenUrl;
import com.maimang.platform.dsp.service.AccessTokenService;
import com.maimang.platform.dsp.utils.ApiService;
import com.maimang.platform.lang.StringUtils;
import com.maimang.platform.redis.RedisService;
import com.maimang.platform.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname AccessTokenServiceImpl
 * @description <br>AccessToken服务实现</br>
 * @date 2019-11-12 16:33
 * @email jintaozhao@qq.com
 */
@Service
public class AccessTokenServiceImpl implements AccessTokenService {

	@Autowired
	private DictService dict;

	@Autowired
	private RedisService redisService;

	@Autowired
	private ApiService apiService;

	/**
	 * 获取Access-Token
	 *
	 */
	@Override
	public String getAccessToken() {
		Map param = new HashMap();
		String appId = dict.getValue("dsp_app_info", "app_id");
		String secret = dict.getValue("dsp_app_info", "secret");
		param.put("app_id", appId);
		param.put("secret", secret);
		param.put("grant_type", "auth_code");
		String authCodeKey = "auth_code";
		if (RedisUtils.exists(authCodeKey)) {
			System.out.println(redisService.get(authCodeKey));
			param.put("auth_code", RedisUtils.getStr(authCodeKey));
			JSONObject resultObject = apiService.sendPost(TokenUrl.ACCESS_TOKEN, param);
			return handleResult(resultObject);
		}

		return StringUtils.EMPTY;
	}

	/**
	 * 获取Refresh-Token
	 *
	 * @return refresh-token
	 */
	@Override
	public String getRefreshToken() {
		Map param = new HashMap();
		String appId = dict.getValue("dsp_app_info", "app_id");
		String secret = dict.getValue("dsp_app_info", "secret");
		param.put("app_id", appId);
		param.put("secret", secret);
		param.put("grant_type", "refresh_token");
		String refreshKey = "refresh_token";
		if (RedisUtils.exists(refreshKey)) {
			String refreshToken = RedisUtils.getStr(refreshKey);
			if (StringUtils.isNotEmpty(refreshToken)) {
				param.put("refresh_token", refreshToken);
				JSONObject resultObject = apiService.sendPost(TokenUrl.REFRESH_TOKEN, param);
				return handleResult(resultObject);
			}
		}

		return StringUtils.EMPTY;
	}

	/**
	 * @return ACCESS-TOKEN
	 */
	@Override
	public String accessToken() {
		String accessKey = "access_token";
		String accessToken;
		if (RedisUtils.exists(accessKey)) {
			accessToken = RedisUtils.getStr(accessKey);
		} else {
			accessToken = this.getAccessToken();
		}
		return accessToken;
	}

	private String getAppId() {
		if (!redisService.exists("app_id")) {
			String appId = dict.getValue("dsp_app_info", "app_id");
			redisService.set("app_id", appId);
		}

		return redisService.get("app_id");
	}

	private String getSecret() {
		if (!redisService.exists("secret")) {
			String secret = dict.getValue("dsp_app_info", "secret");
			redisService.set("secret", secret);
		}

		return redisService.get("secret");
	}

	/**
	 * 解析处理结果
	 *
	 * @param resultObject 请求结果对象
	 * @return
	 */
	private String handleResult(JSONObject resultObject) {
		if (resultObject != null && resultObject.getInteger("code") == 0) {
			JSONObject data = resultObject.getJSONObject("data");
			String accessToken = data.getString("access_token");
			long expiresIn = data.getLongValue("expires_in");
			String refreshToken = data.getString("refresh_token");
			long refreshExpiresIn = data.getLongValue("refresh_token_expires_in");
			String advertiserId = data.getString("advertiser_id");
			// access_token refresh_token 存放到Redis中
			if (StringUtils.isEmpty(advertiserId)) {
				advertiserId = RedisUtils.getStr("advertiser_id");
			}
			String accessKey = "access_token";
			String refreshKey = "refresh_token";
			RedisUtils.set(accessKey, accessToken, expiresIn);
			RedisUtils.set(refreshKey, refreshToken, refreshExpiresIn);
			RedisUtils.set("advertiser_id", advertiserId, refreshExpiresIn);
			return accessToken;
		}
		return StringUtils.EMPTY;
	}
}
